package com.lucaswangdev.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class OssUtil {

    /**
     * 上传文件到OSS
     * @param endpoint OSS访问域名，例如：https://oss-cn-hangzhou.aliyuncs.com
     * @param accessKeyId 阿里云账号的AccessKey ID
     * @param accessKeySecret 阿里云账号的AccessKey Secret
     * @param securityToken 临时访问凭证的安全令牌
     * @param bucketName OSS Bucket名称
     * @param objectName OSS中的目标文件路径
     * @param localFilePath 本地文件路径
     */
    public void uploadFile(String endpoint, String accessKeyId, String accessKeySecret,
                           String securityToken, String bucketName, String objectName, String localFilePath) {

        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret, securityToken);

        try {
            // 创建PutObjectRequest对象
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, new File(localFilePath));

            // 上传文件
            ossClient.putObject(putObjectRequest);
            // ossClient.gener
            // 设置签名URL过期时间，单位为毫秒。本示例以设置过期时间为1小时为例。
            Date expiration = new Date(new Date().getTime() + 3600 * 1000L);
            // 生成以GET方法访问的签名URL。本示例没有额外请求头，其他人可以直接通过浏览器访问相关内容。
            URL url = ossClient.generatePresignedUrl(bucketName, objectName, expiration);
            System.out.println("File uploaded successfully.");
            System.out.println("File uploaded url:\n" + url);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, " +
                    "but was rejected with an error response for some reason.");
            System.out.println("Error Message: " + oe.getErrorMessage());
            System.out.println("Error Code: " + oe.getErrorCode());
            System.out.println("Request ID: " + oe.getRequestId());
            System.out.println("Host ID: " + oe.getHostId());
        } catch (Exception e) {
            System.out.println("Caught an unexpected exception: " + e.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
    public List<String> gerFileUrl(String endpoint, String accessKeyId, String accessKeySecret,
                                   String securityToken, String bucketName, List<String> objectNameList) {
        // String objectName = "img/exampletest8.txt";
        List<String> result = new ArrayList<>();

        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret, securityToken);

        try {
            // 设置签名URL过期时间，单位为毫秒。本示例以设置过期时间为1小时为例。
            Date expiration = new Date(new Date().getTime() + 3600 * 1000L);
            for(String objectName : objectNameList) {
                // 生成以GET方法访问的签名URL。本示例没有额外请求头，其他人可以直接通过浏览器访问相关内容。
                URL url = ossClient.generatePresignedUrl(bucketName, objectName, expiration);
                result.add(String.valueOf(url));
            }
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, " +
                    "but was rejected with an error response for some reason.");
            System.out.println("Error Message: " + oe.getErrorMessage());
            System.out.println("Error Code: " + oe.getErrorCode());
            System.out.println("Request ID: " + oe.getRequestId());
            System.out.println("Host ID: " + oe.getHostId());
        } catch (Exception e) {
            System.out.println("Caught an unexpected exception: " + e.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return result;
    }

    public static String getPreSignUrl(OSS ossClient, String bucketName, String objectName) {
        // String objectName = "img/exampletest8.txt";
        String result = "";
        try {
            // 设置签名URL过期时间，单位为毫秒。本示例以设置过期时间为1小时为例。
            Date expiration = new Date(new Date().getTime() + 3600 * 1000L);

            // 生成以GET方法访问的签名URL。本示例没有额外请求头，其他人可以直接通过浏览器访问相关内容。
            URL url = ossClient.generatePresignedUrl(bucketName, objectName, expiration);

            result = String.valueOf(url);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, " +
                    "but was rejected with an error response for some reason.");
            System.out.println("Error Message: " + oe.getErrorMessage());
            System.out.println("Error Code: " + oe.getErrorCode());
            System.out.println("Request ID: " + oe.getRequestId());
            System.out.println("Host ID: " + oe.getHostId());
        } catch (Exception e) {
            System.out.println("Caught an unexpected exception: " + e.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return result;
    }
}
